<?php
/*
* Update class
*
* Install instructions:
* 1. Create a folder (in this case update)
* 2. Upload the sendchunk.php to the place you want
* 3. Run this file
*
* And remember this is not secure, make it secure yourself!bb
*
* Copyright (c) 2011, Bram Mittendorff
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*    This product includes software developed by the Bram Mittendorff.
* 4. Neither the name of the <organization> nor the
*    names of its contributors may be used to endorse or promote products
*    derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Bram Mittendorff ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Bram Mittendorff BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
*
* */
class chatupdate {
	
	public function createFilePost($file,$sendtodir="",$url,$overwrite=true,$type="php") {
		if($type == "curl") {
			$file = getcwd().'/'.$file;
			$ch = curl_init($url);
			curl_setopt($ch, CURLOPT_POSTFIELDS, array("file"=>"@$file","overwrite"=>$overwrite,"sendtodir"=>$sendtodir));
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$postResult = curl_exec($ch);
			curl_close($ch);
		}
		else {
			$randomstring 		= substr(str_shuffle(str_repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",8)),0,8);
			$aExtra				= array("overwrite"=>$overwrite,"sendtodir"=>$sendtodir);
			$before				= "";
			foreach($aExtra as $key => $value) {
				$before .= "--$randomstring\n";
				$before .= "Content-Disposition: form-data; name=".$key."\n\n".$value."\n";
			}
			$before .= 	"--".$randomstring."\nContent-Disposition: form-data; name=\"file\"; filename=\"$file\"\n\n".file_get_contents($file)."\n\n--$randomstring--\n";
			$params = array("http" => array(
	           "method" => "POST",
	           "header" => "Content-Type: multipart/form-data; boundary=".$randomstring,
	           "content" => $before
			));
			$ctx = stream_context_create($params);
			$fp = fopen($url, "rb", false, $ctx);
			if(!$fp) {
				throw new Exception("Problem with $url, $php_errormsg");
			}
			$response = @stream_get_contents($fp);
			if($response === false) {
				throw new Exception("Problem reading data from $url, $php_errormsg");
			}
			return $response;
		}
	}
	
	public function chmodAll($dir) {
		if(is_dir($dir)) {
			if($handle = opendir($dir)) {
				while(false !== ($object=readdir($handle))) {
					if($object != "." && $object != "..") {
						if(is_dir($dir."/".$object)) {
							chmod($dir."/".$object, 0755);
							$this->chmodAll($dir."/".$object);
						}
						else {
							chmod($dir."/".$object, 0755);
						}
					}
				}
			}
		}
	}
	
	public function sendUpdate($dir,$overwrite=true,$type="php") {
		if(is_dir($dir)) {
			if($handle = opendir($dir)) {
				while(false !== ($object=readdir($handle))) {
					if($object != "." && $object != "..") {
						if(is_dir($dir."/".$object)) {
							$urlFriendly = urlencode($dir."/".$object);
							$fh = fopen("http://zpilman.com/updatemanager/sendchunk.php?folder=$urlFriendly", "r");
							fclose($fh);
							$this->sendUpdate($dir."/".$object,$overwrite,$type);
						}
						else {
							$this->createFilePost($dir."/".$object,$dir."/","http://zpilman.com/updatemanager/sendchunk.php",$overwrite,$type);
						}
					}
				}
			}
		}
	}
	
}